$(function () {
    var table=document.getElementById("tbody-result");
    $.ajax({
        type:'GET',
        dataType:'json',
        url:contextPath+"all/dir",
        success:function (data) {
            // console.log(data);
            // console.log(data.count);
            // console.log(data);
            var str="";
            // var l=data.message.length();
            // console.log(l);
            for (var i=0;i<data.count;i++) {
                // console.log(data.list[i].course_name);
                str += "<tr>" +
                    "<td>" + i + "</td>" +
                    "<td>" + data.list[i].name + "</td>" +
                    "<td><button onclick='chuan(this)'>管理小类</button></td>"+
                    "<td><button onclick='update(this)'>修改</button><button onclick='save(this)'>保存</button><button onclick='del(this)'>删除</button></td>"
                    //         "<td>" + data.list[i].phone+ "</td>" +
                    //         "<td>" + data.list[i].sex + "</td>" +
                    //         "<td>" + data.list[i].userid + "</td>" +
                    //         "<td><button onclick='chuan(this)'>查看资料</button><button onclick='chuanv(this)'>查看视频</button><button onclick='chuand(this)'>查看文档</button><button onclick='save(this)'>保存</button><button onclick='update(this)'>修改</button><button onclick='del(this)'>删除</button></td>" +
                    "</tr>";
            }
            table.innerHTML=str;
        }
        ,
        error:function () {
            alert("查询失败")
        }
    })
});
function chuan(r) {
    var table =window.document.getElementById("tbody-result");
    var i= r.parentNode.parentNode.rowIndex-1;
    var ss =new Object();
    ss.course_dir=table.rows[i].cells[1].innerHTML;
    window.location.href="subjectQuery.html?name="+ss.course_dir;
}
function update(r) {
    var table=document.getElementById("tbody-result");
    var i=r.parentNode.parentNode.rowIndex-1;
    name=table.rows[i].cells[1].innerHTML;
    table.rows[i].cells[1].innerHTML="<input type='text' id='name' value=" + table.rows[i].cells[1].innerHTML+ " />";
}
function save(r) {
    var table=document.getElementById("tbody-result");
    var i=r.parentNode.parentNode.rowIndex-1;
    var ss=new Object();
    ss.course_dir=document.getElementById("name").value;
    $.ajax({
        type:'POST',
        url:contextPath+"new/dir/"+name,
        async: false,
        contentType: "application/json; charset=utf-8",
        data:JSON.stringify(ss),
        dataType:"json",
        success:function (data) {
            alert(data.message)
            window.location.reload();
        },
        error:function () {
            alert("修改失败");
        }
    })
}
function del(r) {
    var table=document.getElementById("tbody-result");
    var ss=new Object();
    var i=r.parentNode.parentNode.rowIndex-1;
     ss.name=table.rows[i].cells[1].innerHTML;
     console.log(ss.name);
    $.ajax({
        type:'DELETE',
        url:contextPath+"delete/dir/"+ss.name,
        success:function (data) {
            alert(data.message)
            window.location.reload();
        },
        error:function () {
            alert("删除失败");
        }
    })
}
$('#btn').on('click',function () {
    var table=document.getElementById("tbody-result");
    var ss=new Object();
    ss.name=document.getElementById("menuname").value;
    $.ajax({
        type:'POST',
        url:contextPath+"find/dir/"+ss.name,
        success:function (data) {
        if(data.message=="存在"){
            var str="<tr>"+
                "<td>"+0+"</td>"+
                "<td>"+ss.name+"</td>"+
                "<td><button>管理小类</button></td>"+
                "<td><button onclick='update(this)'>修改</button><button onclick='save(this)'>保存</button><button onclick='del(this)'>删除</button></td>"+
                "</tr>";
        }
        table.innerHTML=str;
        },
        error:function () {
            alert("查询失败");
        }
    })
});
$('#add').on('click',function () {
    var ss=new Object();
    ss.course_dir=document.getElementById("menuname").value;
    $.ajax({
        type:'POST',
        url:contextPath+"new/dir",
        async: false,
        contentType: "application/json; charset=utf-8",
        data:JSON.stringify(ss),
        dataType:"json",
        success:function (data) {
            alert(data.message);
            window.location.reload();
        },
        error:function () {
            alert("添加失败");
        }
    })
});
